﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   Camera. Very basic camera that helps panning as player moves
// Student Number:     S10096666C
// Student Name:       Trinh Quang Anh

//////////////////////////////////////////////////////////////////////////

using Microsoft.Xna.Framework;

namespace Game
{
    public class Camera
    {
        private Matrix _transform;

        private readonly int _viewportW, _viewportH;
        private readonly Vector3 _translate;

        public Camera(int viewportW, int viewportH)
        {
            Zoom = 1.0f;
            Position = Vector2.Zero;

            _viewportW = viewportW;
            _viewportH = viewportH;
            _translate = new Vector3(viewportW *0.5f, viewportH *0.5f, 0);
            
        }

        public Matrix Transform
        {
            get { return _transform; }
        }

        public Vector2 Position { get; private set; }

        public float Zoom { get; set; }

        public void Move(Vector2 d)
        {
            Position = Position + d;
        }

        public Matrix GetTranformation()
        {
            _transform = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0))*
                         Matrix.CreateScale(new Vector3(Zoom, Zoom, 1))*
                         Matrix.CreateTranslation(_translate);

            return _transform;
        }

        /*
         // Because map spans along X axis, a quick X comparison can eliminate physics check and rendering
        public bool IsInView(IPlatform platform)
        {
            if (platform.Position.X + platform.Width < Position.X || platform.Position.X > Position.X + _viewportW)
                return false;
            // TODO: add Y check
            return true;
        }
         */

        public void SetPosition(Vector2 position)
        {
            position.Y -= _viewportH/2f - 150f;
            Position = position;
        }
    }
}
